********************************************************************************
* Authors: Henrik Andersson & Sirus Dehdari
* Description: This script creates 1000 random samples of all the workers having
* at least 1 workplace co-workers. The sample size for each year depends on the
* number of workers who have at least 1 SAME-SKILL co-worker (usually around 
* 3.1 million)
********************************************************************************

clear all


cd "E:\"

* Setting path to temp folder:
local out_temp "E:\ProjData\IntegrationSD\temp\"

* Command for randomly sampling the data without dropping observations:
ssc install randomtag


use `out_temp'workplace_2006, clear
sum matchedcw
keep LopNr sh_im_cw_noi_swe
drop if LopNr == .

foreach r of num 1/1000{
randomtag, count(3143052) gen(rand_`r')
}

foreach var of varlist rand_*{
quietly replace `var' = . if `var' == 0
quietly replace `var' = sh_im_cw_noi_swe if `var' == 1
}

save `out_temp'random_bmark_workplace_2006, replace



use `out_temp'workplace_2010, clear
sum matchedcw
keep LopNr sh_im_cw_noi_swe
drop if LopNr == .

foreach r of num 1/1000{
randomtag, count(3155773) gen(rand_`r')
}

foreach var of varlist rand_*{
quietly replace `var' = . if `var' == 0
quietly replace `var' = sh_im_cw_noi_swe if `var' == 1
}

save `out_temp'random_bmark_workplace_2010, replace




use `out_temp'workplace_2014, clear
sum matchedcw
keep LopNr sh_im_cw_noi_swe
drop if LopNr == .

foreach r of num 1/1000{
randomtag, count(2640578) gen(rand_`r')
}

foreach var of varlist rand_*{
quietly replace `var' = . if `var' == 0
quietly replace `var' = sh_im_cw_noi_swe if `var' == 1
}

save `out_temp'random_bmark_workplace_2014, replace




///Finally use workplace data that we took out; keep only workplace ID and 
///basic worker info
foreach y in 2006 2010 2014 {
use "E:\ProjData\IntegrationSD\temp\workplace_`y'.dta"

*Variables to keep:
keep LopNr ArbstId im_coworker coworkers share_im_cworker

*Keep only 1 obs per workplace
bysort ArbstId: gen sort = _n
keep if sort ==1

*drop redundant
drop sort LopNr
drop if ArbstId==.

save "E:\ProjData\IntegrationSD\temp\workplace_only_`y'.dta", replace
}



